package cn.springmvc.dao;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import cn.springmvc.common.datasource.DataSourceEnum;
import cn.springmvc.common.datasource.DynamicDataSourceHolder;
import cn.springmvc.entity.User;
import cn.springmvc.repository.UserRepository;

/**
 * @author Vincent.wang
 *
 */
@Repository
public class UserDaoService {

    @Autowired
    protected UserRepository userRepository;

    public User findLocalUserByName(String username) {
        DynamicDataSourceHolder.setDataSourceKey(DataSourceEnum.MASTER.getKey());
        return userRepository.findLocalUserByName(username);
    }

    public List<User> findUserByShop(String id) {
        DynamicDataSourceHolder.setDataSourceKey(DataSourceEnum.MASTER.getKey());
        return userRepository.findUserByShop(id);
    }

    public void updateUserLastLoginTime(User user) {
        DynamicDataSourceHolder.setDataSourceKey(DataSourceEnum.MASTER.getKey());
        userRepository.updateUserLastLoginTime(user);
    }

    public List<User> findUsers() {
        DynamicDataSourceHolder.setDataSourceKey(DataSourceEnum.MASTER.getKey());
        return userRepository.findUsers();
    }

    public List<User> findEmp(String shopId, String empName) {
        DynamicDataSourceHolder.setDataSourceKey(DataSourceEnum.MASTER.getKey());
        return userRepository.findEmp(shopId, empName);
    }

    public void add(User user) {
        DynamicDataSourceHolder.setDataSourceKey(DataSourceEnum.MASTER.getKey());
        userRepository.add(user);
    }

    public User findUserById(String id) {
        DynamicDataSourceHolder.setDataSourceKey(DataSourceEnum.MASTER.getKey());
        return userRepository.findUserById(id);
    }

    public void updatePassword(User u) {
        DynamicDataSourceHolder.setDataSourceKey(DataSourceEnum.MASTER.getKey());
        userRepository.updatePassword(u);
    }

}